package com.bianguo.android.iocdemo.ui;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class UIHelper {

    private static volatile UIHelper instance;
    //标准设备信息（美工根据什么尺寸做的图）
    public static final float STANDARD_WIDTH = 1080f;
    public static final float STANDARD_HEIGHT = 1920f;
    //实际设备信息（app运行的手机屏幕尺寸）
    public static float displayMetricsWidth;
    public static float displayMetricsHeight;

    private int statusBarHeight;

    private UIHelper() {}

    private UIHelper(Context mContext) {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        if (displayMetricsWidth == 0.0f || displayMetricsHeight == 0.0f) {
            wm.getDefaultDisplay().getMetrics(dm);
            //获取状态栏高度
            statusBarHeight = getStatusBarHeight(mContext);
            if (dm.widthPixels > dm.heightPixels) {
                //横屏
                this.displayMetricsWidth = (float) (dm.heightPixels);
                this.displayMetricsHeight = (float) (dm.widthPixels - statusBarHeight);
            } else {
                //竖屏
                this.displayMetricsWidth = (float) (dm.widthPixels);
                this.displayMetricsHeight = (float) (dm.heightPixels - statusBarHeight);
            }
        }
    }

    public static UIHelper getInstance() {
        if (instance == null) {
            throw new RuntimeException("");
        }
        return instance;
    }

    public static UIHelper getInstance(Context mContext) {
        UIHelper helper = instance;
        if (helper == null) {
            synchronized (UIHelper.class) {
                if (helper == null) {
                    helper = new UIHelper(mContext);
                    instance = helper;
                }
            }
        }
        return helper;
    }

    public static UIHelper notifyInstance(Context mContext) {
        instance = new UIHelper(mContext);
        return instance;
    }

    private int getStatusBarHeight(Context mContext) {
        int result = 48;
        Resources resources = mContext.getResources();
        int identifier = resources.getIdentifier("status_bar_height", "dimens", "android");
        if (identifier > 0) {
            result = resources.getDimensionPixelSize(identifier);
        }
        return result;
    }

    /**
     * 获取宽的缩放比例
     * @return
     */
    public float getHorizontalScaleValue() {
        return displayMetricsWidth / STANDARD_WIDTH;
    }

    /**
     * 获取高的缩放比例
     * @return
     */
    public float getVerticalScaleValue() {
        return displayMetricsHeight / (STANDARD_HEIGHT - statusBarHeight);
    }

    /**
     * 获取手机上显示的宽度
     * @param width 美工给的宽度
     * @return
     */
    public int getWidth(int width) {
        return Math.round((float) width * getHorizontalScaleValue());
    }

    /**
     * 获取手机上显示的高度
     * @param height 美工给的高度
     * @return
     */
    public int getHeight(int height) {
        return Math.round((float) height * getVerticalScaleValue());
    }
}
